//
//  RegisterViewController.m
//  MingShi
//
//  Created by MJiao on 15/7/1.
//  Copyright (c) 2015年 MJiao. All rights reserved.
//

#import "RegisterViewController.h"

#import "KVNProgress.h"
#import "UserManager.h"
#import "AFHTTPRequestOperationManager.h"
#import "User.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)btnRegisterAction:(id)sender {


    NSString *account = [self.tfUserAccount text];
    NSString *pwd = [self.tfPwd text];
    NSString *confirmPwd = [self.lbConfirmPwd text];

    if (account == nil || account.length == 0) {
        [KVNProgress showErrorWithStatus:@"请输入用户名"];
        [self.tfUserAccount becomeFirstResponder];
        return;
    }
    if (pwd == nil || pwd.length == 0) {
        [KVNProgress showErrorWithStatus:@"请输入密码"];
        [self.tfPwd becomeFirstResponder];
        return;
    }
    if (confirmPwd == nil || confirmPwd.length == 0) {
        [KVNProgress showErrorWithStatus:@"请再次输入密码"];
        [self.lbConfirmPwd becomeFirstResponder];
        return;
    }
    if(![pwd isEqualToString: confirmPwd]){
        [KVNProgress showErrorWithStatus:@"密码不匹配"];
        [self.lbConfirmPwd becomeFirstResponder];
        return;
    }

    // Adds a status below the circle
    [KVNProgress showWithStatus:@"注册中..."];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];


    NSString *encodedAccount = [account stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSString *urlString = [NSString stringWithFormat:@"http://121.41.91.36:8888/api/users/sms?loingName=%@&pssword=%@", encodedAccount, pwd];


    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        // 结束登陆提示
        [KVNProgress dismiss];

        NSString *code = [responseObject objectForKey:@"code"];
        NSString *msg = [responseObject objectForKey:@"msg"];

        //登录失败
        if (code == nil || ![code isEqualToString:@"00"]) {
            [KVNProgress showErrorWithStatus:msg];
            return;
        }

        [KVNProgress showSuccessWithStatus:msg completion:^{
            // Things you want to do after the HUD is gone.
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        // 结束登陆提示
        [KVNProgress dismiss];
        [KVNProgress showErrorWithStatus:@"网络错误"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
